/**
* @file hash_utils.cpp
*
* Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "hash_utils.h"

namespace acl {
namespace hash_utils {
// use aclTensorDesc to calculate a hash seed according to HashCombine function
aclError GetTensorDescHash(const int32_t num, const aclTensorDesc *const descArr[], size_t &seed)
{
    if ((num > 0) && (descArr == nullptr)) {
        ACL_LOG_ERROR("[Check][Params] param descArr must not be null");
        return ACL_ERROR_FAILURE;
    }
    HashCombine(seed, num);
    for (int32_t i = 0; i < num; ++i) {
        ACL_REQUIRES_NOT_NULL(descArr[i]);
        HashCombine(seed, static_cast<int32_t>(descArr[i]->dataType));
        HashCombine(seed, static_cast<int32_t>(descArr[i]->format));
        if (descArr[i]->storageFormat != ACL_FORMAT_UNDEFINED) {
            HashCombine(seed, static_cast<int32_t>(descArr[i]->storageFormat));
        }
        for (auto &dim : descArr[i]->dims) {
            HashCombine(seed, dim);
        }
        for (auto &shapeRange : descArr[i]->shapeRange) {
            HashCombine(seed, shapeRange.first);
            HashCombine(seed, shapeRange.second);
        }
        HashCombine(seed, descArr[i]->isConst);
        HashCombine(seed, static_cast<int32_t>(descArr[i]->memtype));
    }
    return ACL_SUCCESS;
}

aclError GetTensorDescHashDynamic(const int32_t num, const aclTensorDesc *const descArr[], size_t &seed)
{
    if ((num > 0) && (descArr == nullptr)) {
        ACL_LOG_ERROR("[Check][Params] param descArr must not be null");
        return ACL_ERROR_FAILURE;
    }
    HashCombine(seed, num);
    for (int32_t i = 0; i < num; ++i) {
        ACL_REQUIRES_NOT_NULL(descArr[i]);
        HashCombine(seed, static_cast<int32_t>(descArr[i]->dataType));
        HashCombine(seed, static_cast<int32_t>(descArr[i]->format));
        if (descArr[i]->storageFormat != ACL_FORMAT_UNDEFINED) {
            HashCombine(seed, static_cast<int32_t>(descArr[i]->storageFormat));
        }
        HashCombine(seed, descArr[i]->isConst);
        HashCombine(seed, static_cast<int32_t>(descArr[i]->memtype));
    }
    return ACL_SUCCESS;
}

aclError GetAclOpHash(const AclOp &aclOp, const aclopAttr *const opAttr, const size_t attrDigest, size_t &seed)
{
    // Init seed to maintain consistency between Insert and Get
    seed = 0U;

    HashCombine(seed, aclOp.opType);

    ACL_REQUIRES_OK(GetTensorDescHash(aclOp.numInputs, aclOp.inputDesc, seed));

    ACL_REQUIRES_OK(GetTensorDescHash(aclOp.numOutputs, aclOp.outputDesc, seed));

    HashCombine(seed, attrDigest);
    // need use tmp opAttr, because aclop.opAttr may not be matched
    if (opAttr != nullptr) {
        for (auto &constBuf : opAttr->GetConstBuf()) {
            HashCombine(seed, constBuf);
        }
    }
    return ACL_SUCCESS;
}

aclError GetAclOpHashDynamic(const AclOp &aclOp, const aclopAttr *const opAttr, const size_t attrDigest,
                             size_t &seed, const uint64_t seq)
{
    // Init seed to maintain consistency between Insert and Get
    seed = 0U;

    HashCombine(seed, aclOp.opType);
    HashCombine(seed, seq);

    ACL_REQUIRES_OK(GetTensorDescHashDynamic(aclOp.numInputs, aclOp.inputDesc, seed));

    ACL_REQUIRES_OK(GetTensorDescHashDynamic(aclOp.numOutputs, aclOp.outputDesc, seed));

    HashCombine(seed, attrDigest);
    // need use tmp opAttr, because aclop.opAttr may not be matched
    if (opAttr != nullptr) {
        for (auto &constBuf : opAttr->GetConstBuf()) {
            HashCombine(seed, constBuf);
        }
    }
    return ACL_SUCCESS;
}

aclError CalculateSimpleHash(const char *filePath, std::string &hashResult) {
    ACL_LOG_INFO("Begin to calculate hash for file: %s", filePath);

    std::string fileContent;

    // 文件路径为空，按照空文件处理
    if (filePath != nullptr && strlen(filePath) != 0UL) {
        std::ifstream file(filePath, std::ios::binary);
        if (!file.is_open()) {
            ACL_LOG_ERROR("Failed to open file: %s", filePath);
            return ACL_ERROR_INVALID_FILE;
        }

        std::stringstream buffer;
        buffer << file.rdbuf();
        fileContent = buffer.str();
        file.close();  // 显式关闭文件
    }

    std::size_t hashValue = std::hash<std::string>{}(fileContent);
    hashResult = std::to_string(hashValue);
    ACL_LOG_INFO("file: %s, hash: %s", filePath, hashResult.c_str());

    return ACL_SUCCESS;
}

} // namespace hash_utils
} // namespace acl